home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 August / ENTER.ISO / files / gimp-2.0.5-i586-setup.exe / {app} / share / gimp / 2.0 / scripts / add-bevel.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2004-09-26  |  6.9 KB  |  194 lines

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ;
  4. ; add-bevel.scm version 1.04
  5. ; Time-stamp: <2004-02-09 17:07:06 simon>
  6. ;
  7. ; This program is free software; you can redistribute it and/or modify
  8. ; it under the terms of the GNU General Public License as published by
  9. ; the Free Software Foundation; either version 2 of the License, or
  10. ; (at your option) any later version.
  11. ;
  12. ; This program is distributed in the hope that it will be useful,
  13. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ; GNU General Public License for more details.
  16. ;
  17. ; You should have received a copy of the GNU General Public License
  18. ; along with this program; if not, write to the Free Software
  19. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ;
  21. ; Copyright (C) 1997 Andrew Donkin  (ard@cs.waikato.ac.nz)
  22. ; Contains code from add-shadow.scm by Sven Neumann
  23. ; (neumanns@uni-duesseldorf.de) (thanks Sven).
  24. ;
  25. ; Adds a bevel to an image.  See http://www.cs.waikato.ac.nz/~ard/gimp/
  26. ;
  27. ; If there is a selection, it is bevelled.
  28. ; Otherwise if there is an alpha channel, the selection is taken from it
  29. ; and bevelled.
  30. ; Otherwise the whole image is bevelled.
  31. ;
  32. ; The selection is set on exit, so Select->Invert then Edit->Clear will
  33. ; leave a cut-out.  Then use Sven's add-shadow for that
  34. ; floating-bumpmapped-texture cliche.
  35.  
  36. ;
  37. ; 1.01: now works on offset layers.
  38. ; 1.02: has crop-pixel-border option to trim one pixel off each edge of the
  39. ;       bevelled image.  Bumpmapping leaves edge pixels unchanged, which
  40. ;       looks bad.  Oddly, this is not apparant in the GIMP - you have to
  41. ;       save the image and load it into another viewer.  First noticed in
  42. ;       Nutscrape.
  43. ;       Changed path (removed "filters/").
  44. ; 1.03: adds one-pixel border before bumpmapping, and removes it after.
  45. ;       Got rid of the crop-pixel-border option (no longer reqd).
  46. ; 1.04: Fixed undo handling, ensure that bumpmap is big enough,
  47. ;       (instead of resizing the image). Removed references to outdated
  48. ;       bumpmap plugin.     (Simon)
  49. ;
  50.  
  51. (define (script-fu-add-bevel img
  52.                              drawable
  53.                              thickness
  54.                              work-on-copy
  55.                              keep-bump-layer)
  56.  
  57.   (let* ((index 0)
  58.          (bevelling-whole-image FALSE)
  59.          (greyness 0)
  60.          (thickness (abs thickness))
  61.          (type (car (gimp-drawable-type-with-alpha drawable)))
  62.          (image (if (= work-on-copy TRUE) (car (gimp-image-duplicate img)) img))
  63.          (pic-layer (car (gimp-image-get-active-drawable image)))
  64.          (offsets (gimp-drawable-offsets pic-layer))
  65.          (width (car (gimp-drawable-width pic-layer)))
  66.          (height (car (gimp-drawable-height pic-layer)))
  67.          (old-bg (car (gimp-palette-get-background)))
  68.          ; Bumpmap has a one pixel border on each side
  69.          (bump-layer (car (gimp-layer-new image
  70.                                           (+ width 2)
  71.                                           (+ height 2)
  72.                                           GRAY
  73.                                           "Bumpmap"
  74.                                           100
  75.                                           NORMAL-MODE)))
  76.          )
  77.     
  78.     ; If the layer we're bevelling is offset from the image's origin, we
  79.     ; have to do the same to the bumpmap
  80.     (gimp-layer-set-offsets bump-layer (- (car offsets) 1)
  81.                                        (- (cadr offsets) 1))
  82.  
  83.     ; disable undo on copy, start group otherwise
  84.     (if (= work-on-copy TRUE)
  85.       (gimp-image-undo-disable image)
  86.       (gimp-image-undo-group-start image)
  87.     )
  88.  
  89.     ;------------------------------------------------------------
  90.     ;
  91.     ; Set the selection to the area we want to bevel.
  92.     ;
  93.     (if (eq? 0 (car (gimp-selection-bounds image)))
  94.         (begin
  95.           (set! bevelling-whole-image TRUE) ; ...so we can restore things properly, and crop.
  96.           (if (car (gimp-drawable-has-alpha pic-layer))
  97.               (gimp-selection-layer-alpha pic-layer)
  98.               (gimp-selection-all image)
  99.           )
  100.         )
  101.      )
  102.  
  103.     ; Store it for later.
  104.     (set! select (car (gimp-selection-save image)))
  105.     ; Try to lose the jaggies
  106.     (gimp-selection-feather image 2)
  107.  
  108.     ;------------------------------------------------------------
  109.     ;
  110.     ; Initialise our bumpmap
  111.     ;
  112.     (gimp-palette-set-background '(0 0 0))
  113.     (gimp-drawable-fill bump-layer BACKGROUND-FILL)
  114.  
  115.     (set! index 1)
  116.     (while (< index thickness)
  117.            (set! greyness (/ (* index 255) thickness))
  118.            (gimp-palette-set-background (list greyness greyness greyness))
  119.            ;(gimp-selection-feather image 1) ;Stop the slopey jaggies?
  120.            (gimp-edit-bucket-fill bump-layer BG-BUCKET-FILL NORMAL-MODE
  121.                                   100 0 FALSE 0 0)
  122.            (gimp-selection-shrink image 1)
  123.            (set! index (+ index 1))
  124.     )
  125.     ; Now the white interior
  126.     (gimp-palette-set-background '(255 255 255))
  127.     (gimp-edit-bucket-fill bump-layer BG-BUCKET-FILL NORMAL-MODE
  128.                            100 0 FALSE 0 0)
  129.  
  130.     ;------------------------------------------------------------
  131.     ;
  132.     ; Do the bump.
  133.     ;
  134.     (gimp-selection-none image)
  135.  
  136.     ; To further lessen jaggies?
  137.     ;(plug-in-gauss-rle 1 image bump-layer thickness TRUE TRUE)
  138.  
  139.  
  140.     ;
  141.     ; BUMPMAP INVOCATION:
  142.     ;
  143.     (plug-in-bump-map 1 image pic-layer bump-layer 125 45 3 0 0 0 0 TRUE FALSE 1)
  144.  
  145.     ;------------------------------------------------------------
  146.     ;
  147.     ; Restore things
  148.     ;
  149.     (gimp-palette-set-background old-bg)
  150.     (if (= bevelling-whole-image TRUE)
  151.         (gimp-selection-none image)        ; No selection to start with
  152.         (gimp-selection-load select)
  153.     )
  154.     ; If they started with a selection, they can Select->Invert then
  155.     ; Edit->Clear for a cutout.
  156.  
  157.     ; clean up
  158.     (gimp-image-remove-channel image select)
  159.     (if (= keep-bump-layer TRUE)
  160.         (begin
  161.           (gimp-image-add-layer image bump-layer 1)
  162.           (gimp-drawable-set-visible bump-layer 0))
  163.         (gimp-drawable-delete bump-layer)
  164.     )
  165.  
  166.     (gimp-image-set-active-layer image pic-layer)
  167.  
  168.     ; enable undo / end undo group
  169.     (if (= work-on-copy TRUE) 
  170.       (begin
  171.         (gimp-display-new image)
  172.         (gimp-image-undo-enable image)
  173.       )
  174.       (gimp-image-undo-group-end image)
  175.     )
  176.     (gimp-displays-flush)
  177.  
  178.     )
  179.   )
  180.  
  181. (script-fu-register "script-fu-add-bevel"
  182.                     _"<Image>/Script-Fu/Decor/Add B_evel..."
  183.                     "Add a bevel to an image"
  184.                     "Andrew Donkin <ard@cs.waikato.ac.nz>"
  185.                     "Andrew Donkin"
  186.                     "1997/11/06"
  187.                     "RGB* GRAY*"
  188.                     SF-IMAGE "Image" 0
  189.                     SF-DRAWABLE "Drawable" 0
  190.                     SF-ADJUSTMENT _"Thickness"       '(5 0 30 1 2 0 0)
  191.                     SF-TOGGLE     _"Work on Copy"    TRUE
  192.                     SF-TOGGLE     _"Keep Bump Layer" FALSE
  193.                     )
  194.